﻿#include "user_page_config.h"
#include "lv_drivers/win32drv/win32drv.h"


#define PAGE_SETTING_NUM    5  //定义菜单个数


static lv_obj_t* m_btn[PAGE_SETTING_NUM];
static lv_obj_t* m_lab[PAGE_SETTING_NUM];

/*Create style*/
static lv_style_t style_line;


const lv_point_t line_points1[] = { {45,60},{195,60} };
const lv_point_t line_points2[] = { {45,110},{195,110} };
const lv_point_t line_points3[] = { {45,160},{195,160} };
const lv_point_t line_points4[] = { {45,210},{195,210} };

const char  chinese_setting_char[PAGE_SETTING_NUM][15] =
{
    "配对",
    "重置",
    "关机",
    "主页",
    "语言",
    //"灯光",
    //"设置",
};


const char  english_setting_char[PAGE_SETTING_NUM][15] =
{
    "Paired",
    "Reset",
    "Shut Down",
    "Home",
    "Language",
    //"Light",
    //"Setting",
};



//void user_setting_page_entry(lv_obj_t* obj);
//void user_setting_page_exit(lv_obj_t* obj);



static void setting_event_handler(lv_event_t* e)
{

    //if (page_config.current_page != SETTING_MENU)
    //{
    //    return;
    //}

    lv_event_code_t event = lv_event_get_code(e);
    //lv_obj_t *obj = lv_event_get_target(e);
    lv_obj_t* obj = e->user_data;
    if (!lv_obj_is_valid(obj))
        return;
    if (event == LV_EVENT_DELETE)
    {
       // printf( "LV_EVENT_DELETE\r\n");
    }
    else if (event == LV_EVENT_CLICKED)
    {

        lv_obj_t *obj = lv_event_get_current_target(e);
        uint32_t id = lv_obj_get_index(obj); //获取界面索引选中跳入界面
        printf("LV_EVENT_CLICKED event=%d,child %d\r\n", event,id);

        //推出界面释放内存
        extern lv_obj_t* prj_prev_cont;
        extern lv_obj_t* prj_parent_cont;
        //printf("lv_obj_get_child_cnt = %d\r\n", lv_obj_get_child_cnt(lv_obj_get_parent(obj)));
        lv_obj_t* parent_obj = lv_obj_get_parent(obj);



        switch (id)
        {
        
                case PAIRED_MENU:
                       //user_setting_page_entry(prj_prev_cont);
                    user_setting_page_exit(parent_obj);
                    lv_obj_clean(prj_parent_cont);
                    prj_prev_cont = lv_obj_create(prj_parent_cont);
                     user_paired_page_entry(prj_prev_cont);
                    printf("---------------------PAIRED_MENU-------------------\r\n");
                    break;
                case RESET_MENU:
                    page_config.window_type = DISPLAY_WINDOW_RESET_DEFAULT_NO;
                    user_window_page_entry(prj_prev_cont, page_config.window_type);

                    printf("---------------------RESET_MENU-------------------\r\n");

                    break;
                case SHUTDOWN_MENU:
                    printf("---------------------SHUTDOWN_MENU-------------------\r\n");

                    break;
                case MAIN_MENU:
                    user_setting_page_exit(parent_obj);
                    lv_obj_clean(prj_parent_cont);
                    prj_prev_cont = lv_obj_create(prj_parent_cont);
                    user_main_page_entry(prj_prev_cont);
                    printf("---------------------MAIN_MENU-------------------\r\n");

                    break;
                case LANGUAGE_MENU:
                    printf("---------------------LANGUAGE_MENU-------------------\r\n");

                    break;
                case SETTING_MENU:
                    printf("---------------------SETTING_MENU-------------------\r\n");

                    break;

                default:break;
        }

       
    }

    else if(event == LV_EVENT_FOCUSED)
    {

        lv_obj_t* obj = lv_event_get_current_target(e);
        uint32_t id = lv_obj_get_index(obj); //获取界面索引选中跳入界面
        lv_obj_set_style_border_color(m_btn[id], lv_color_make(0xff, 0xff, 0xff), 0);
        //lv_obj_add_state(m_btn[id], LV_STATE_FOCUS_KEY|LV_STATE_FOCUSED|LV_STATE_PRESSED);
       
        //printf("LV_EVENT_FOCUSED event=%d,id %d\r\n", event,id);

        lv_group_focus_obj(m_btn[id]);
    }
    else if (event == LV_EVENT_DEFOCUSED)
    {
        lv_obj_t* obj = lv_event_get_current_target(e);
        uint32_t id = lv_obj_get_index(obj); //获取界面索引选中跳入界面
        lv_obj_set_style_border_color(m_btn[id], lv_color_make(0x00, 0x00, 0x00), 0);
        //printf("LV_EVENT_DEFOCUSED event=%d,id %d\r\n", event, id);
        //lv_obj_clear_state(obj, LV_STATE_FOCUS_KEY|LV_STATE_FOCUSED|LV_STATE_PRESSED);    /*Remove the pressed state manually*/

    }
    else if (event == LV_EVENT_KEY)
    {
        printf("%s LV_EVENT_KEY event=%d\r\n",__FUNCTION__ ,event);
    }
    else if (event == LV_EVENT_MENU_CHANGE)
    {
        //printf("LV_EVENT_MENU_CHANGE event=%d\r\n", event);
        uint32_t* PAGE_INDEX = lv_event_get_param(e);
        printf("%s LV_EVENT_MENU_CHANGE key code %d\r\n", __FUNCTION__, *PAGE_INDEX);

        page_config.current_page = *PAGE_INDEX;

        Menu_Switch_Callback(page_config.current_page, page_config.last_page);

    }
}



/*
    进入设置界面
*/
void user_setting_page_entry(lv_obj_t* obj)
{
    printf("%s\r\n",__FUNCTION__);

    page_config.current_page = SETTING_MENU;

    lv_obj_set_style_pad_all(obj, 0, 0);
    lv_obj_set_style_border_width(obj, 0, 0);
    lv_obj_set_style_bg_color(obj, lv_color_black(), 0);
    lv_obj_set_size(obj, LV_PCT(100), LV_PCT(100));
    lv_obj_set_scrollbar_mode(obj, LV_SCROLLBAR_MODE_OFF);



    /*Create style*/
    static lv_style_t style_line;

    lv_style_init(&style_line);
    lv_style_set_line_width(&style_line, 2);
    lv_style_set_line_color(&style_line, lv_color_make(0xff, 0xff, 0xff));
    lv_style_set_line_rounded(&style_line, true);


    
   

#if 1




    for (int i = 0; i < PAGE_SETTING_NUM; i++)
    {
        m_btn[i] = lv_btn_create(obj); // 创建一个按钮
        lv_obj_remove_style_all(m_btn[i]);
        lv_obj_set_style_pad_all(m_btn[i], 0, 0);
        lv_obj_set_style_border_width(m_btn[i], 1, 0);
        lv_obj_set_style_border_color(m_btn[i], lv_color_make(0x00, 0x00, 0x00), 0);
        //lv_obj_set_style_pad_all(m_btn[i], 1, 0);
        //lv_obj_set_style_border_color(m_btn[i], lv_color_make(0xff, 0xff, 0xff), LV_PART_SELECTED);

        
        lv_obj_set_style_bg_color(m_btn[i], lv_color_black(), 0);
        //lv_obj_add_style(m_btn[i], &style_line, LV_PART_SELECTED);
      
        if (m_btn[i] != NULL)
        {
            m_lab[i] = lv_label_create(m_btn[i]); // 创建一个标签
            lv_label_set_recolor(m_lab[i], true);                        /*Enable re-coloring by commands in the text*/
            lv_obj_set_style_text_font(m_lab[i], &font18, 0);
            lv_obj_set_style_text_color(m_lab[i], lv_color_make(0xff, 0xff, 0xff), 0);

            if (page_config.language_type == CHINESE)
            {
                lv_label_set_text(m_lab[i], chinese_setting_char[i]);

            }
            if (page_config.language_type == ENGLISH)
            {
                lv_label_set_text(m_lab[i], english_setting_char[i]);

            }
            lv_obj_align(m_btn[i], LV_ALIGN_TOP_MID, 0, 30 + i * 48);

            lv_obj_add_flag(m_btn[i], LV_OBJ_FLAG_CHECKABLE|LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_CLICK_FOCUSABLE);
            //lv_obj_add_flag(m_btn[i], LV_OBJ_FLAG_SCROLL_ON_FOCUS);
            
          
            lv_obj_add_state(m_btn[i], LV_STATE_FOCUSED); /*To be sure the cursor is visible*/
            lv_obj_add_event_cb(m_btn[i], setting_event_handler, LV_EVENT_ALL, m_btn[i]);
         


            //lv_obj_add_style(obj, &style_obj, LV_STATE_PRESSED);  // 在对象被按下时应用样式
         
            lv_obj_clear_flag(m_btn[i],LV_OBJ_FLAG_EVENT_BUBBLE);
            //lv_obj_set_pos(m_lab, 102, 30 + i * 48);
            lv_group_add_obj(g_group, m_btn[i]);
           
        }
    }
#else



    for (int i = 0; i < PAGE_SETTING_NUM; i++)
    {
        lv_obj_t* m_lab = lv_label_create(obj); // 创建一个标签

        if (m_lab != NULL)
        {
            lv_label_set_recolor(m_lab, true);                        /*Enable re-coloring by commands in the text*/
            lv_obj_set_style_text_font(m_lab, &font18, 0);
            lv_obj_set_style_text_color(m_lab, lv_color_make(0xff, 0xff, 0xff), 0);

            if (page_config.language_type == CHINESE)
            {
                lv_label_set_text(m_lab, chinese_setting_char[i]);
               
            }
            if (page_config.language_type == ENGLISH)
            {
                lv_label_set_text(m_lab, english_setting_char[i]);
                
            }
            lv_obj_align(m_lab, LV_ALIGN_TOP_MID, 0, 30 + i * 48);

            lv_obj_add_flag(m_lab,LV_OBJ_FLAG_CLICKABLE| LV_OBJ_FLAG_CLICK_FOCUSABLE);

            lv_obj_add_event_cb(m_lab, setting_event_handler, LV_EVENT_FOCUSED| LV_EVENT_KEY, obj);
            lv_group_add_obj(g_group, m_lab);
            //lv_obj_set_pos(m_lab, 102, 30 + i * 48);
           
            lv_indev_set_group(lv_win32_keypad_device_object,g_group);
        }
    }

#endif





   /*Create a line and apply the new style*/
    lv_obj_t* line1 = NULL;
    line1 = lv_line_create(obj);
    lv_line_set_points(line1, line_points1, 2);     /*Set the points*/
    lv_obj_add_style(line1, &style_line, 0);


    /*Create a line and apply the new style*/
    lv_obj_t* line2 = NULL;
    line2 = lv_line_create(obj);
    lv_line_set_points(line2, line_points2, 2);     /*Set the points*/
    lv_obj_add_style(line2, &style_line, 0);



    /*Create a line and apply the new style*/
    lv_obj_t* line3 = NULL;
    line3 = lv_line_create(obj);
    lv_line_set_points(line3, line_points3, 2);     /*Set the points*/
    lv_obj_add_style(line3, &style_line, 0);


    /*Create a line and apply the new style*/
    lv_obj_t* line4 = NULL;
    line4 = lv_line_create(obj);
    lv_line_set_points(line4, line_points4, 2);     /*Set the points*/
    lv_obj_add_style(line4, &style_line, 0);





   
        lv_group_focus_obj(m_btn[0]);
        lv_obj_set_style_border_color(m_btn[0], lv_color_make(0xff, 0xff, 0xff), 0);
        //lv_obj_add_state(m_btn[0], LV_STATE_FOCUS_KEY);
        //lv_obj_add_state(m_btn[0], LV_STATE_PRESSED);

  
        lv_obj_add_event_cb(obj, setting_event_handler, LV_EVENT_MENU_CHANGE, obj);
    //lv_obj_clear_state(m_btn[0], LV_STATE_FOCUS_KEY|LV_STATE_FOCUSED|LV_STATE_PRESSED);    /*Remove the pressed state manually*/

    //lv_obj_clear_flag(m_btn[0], LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_CLICK_FOCUSABLE);
    //lv_obj_clear_flag(m_btn[0], LV_OBJ_FLAG_SCROLL_ON_FOCUS);
    //extern void user_window_page_entry(lv_obj_t* obj, WINDOW_Type_t type);
    //user_window_page_entry(obj,DISPLAY_WINDOW_ADD_DEV1);

}



/*
    退出释放内存
*/
void user_setting_page_exit(lv_obj_t* obj)
{

    
    if (lv_group_get_obj_count(g_group) > 0)
    {
        printf("user_setting_page_exit grpup num %d\r\n", lv_group_get_obj_count(g_group));
        lv_group_remove_all_objs(g_group);  
    }
    page_config.last_page = page_config.current_page;
    lv_obj_clean(obj);


    //page_config.language_type = !page_config.language_type;
   
}
